home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 671 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.6 KB

  1. Path: gate.net!pslfl2-10
  2. From: bhutto@gate.net (William Hutto)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Packing characters
  5. Date: 8 Jan 1996 11:21:01 GMT
  6. Organization: CyberGate, Inc.
  7. Message-ID: <4cqumt$1quo@news.gate.net>
  8. References: <4cp6st$phk@news.isc.rit.edu> <30F0EFB1.AD3@cnsunix.albany.edu>
  9. NNTP-Posting-Host: pslfl2-10.gate.net
  10. X-Newsreader: News Xpress Version 1.0 Beta #4
  11.  
  12. In article <30F0EFB1.AD3@cnsunix.albany.edu>,
  13.    Nicholas Paldino <np1010@cnsunix.albany.edu> wrote:
  14. >SWANSON wrote:
  15. >> 
  16. >> Problem: I have a value like this: XXX.YYY where the X and Y's
  17. >> are numbers, like in an ip address. The values are 0-255.
  18. >> 
  19. >> What is the best way to store them so they take up the least
  20. >> amount of room? Later I need to traansmit them in a packet
  21. >> header, so space is an issue.
  22. >> 
  23. >> Thanks,
  24. >> 
  25. >> Carl
  26. >> css0958@grace.rit.edu
  27. >
  28. >This is my first time posting here and I have only six months experience 
  29. >with C but I believe that you could store those values in an unsigned 
  30. >char (assuming that a char on your machine uses 8 bits).  Four of those 
  31. >in turn could be packed into an int (using masking and whatnot and 
  32. >assuming that an int is four bytes on your machine).  Good luck.
  33. >
  34. >                - Nicholas Paldino
  35. >                - np1010@cnsunix.albany.edu
  36.  
  37. It sounds like you need to use the ol' union. Given an example.
  38.  
  39. typedef union {
  40.     unsigned char route[2];
  41.     unsigned short address;
  42.     }IP;
  43.  
  44. func()
  45. {
  46. IP ip;
  47.  
  48.     ip.route[0]=getbyte();
  49.     ip.route[1]=getbyte();
  50.     do_something_with(ip.address);
  51. }
  52.  
  53. The union virtually allows more than one data item to occupy the same space.
  54. I illustrate:
  55.  
  56.  
  57. ip.<item>
  58. ------------------------------------------
  59. |                  |                     |
  60. |                  |                     |
  61. |               address                  |
  62. |                  |                     |
  63. |   route[0]       |    route[1]         |
  64. |                  |                     |
  65. ------------------------------------------
  66.  
  67. In this example:
  68. ip.address refers to the whole which is of size unsigned short int.
  69. ip.route[0] refers to the low byte of the whole.
  70. ip.route[1] refers to the high byte of the whole.
  71.  
  72.  
  73. For a full ip address you could use:
  74.  
  75. typedef union {
  76.     unsigned char route[4];
  77.     unsigned long address;
  78.     }IP;
  79.  
  80. Which allows you to break ip.address into 4 individual parts:
  81.     ip.route[0]
  82.     ip.route[1]
  83.     ...
  84.  
  85. The only problem you might have with unions is the dependence of sizeof(type). 
  86. In order for the alignment of these cohabitant objects to be useful, they need 
  87. to be a predecribed size. You may want to include some checking in your code 
  88. to make sure the union elements align properly.
  89.  
  90. I hope that gives you some direction.
  91.  
  92. Bill
  93.  
  94.  
  95. "Whatcha got on?...Your mind?"
  96.